package ru.agiledev.akb.client.panel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.*;
import ru.agiledev.akb.client.AspectKnowledgeBaseApplication;

/**
 * Created by a.kirillov
 * Date: 3/24/11 Time: 11:49 PM
 */
public class PDFInfoPanel extends VerticalPanel {

    private Frame frame;

    public PDFInfoPanel(){

        HorizontalPanel commons_structure = new HorizontalPanel();
        HorizontalPanel examples_article = new HorizontalPanel();
        examples_article.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
        examples_article.setWidth("784px");
        examples_article.setSpacing(4);

        commons_structure.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
        commons_structure.setWidth("784px");
        commons_structure.setSpacing(2);

        AnchorClickHandler clickHandler = new AnchorClickHandler();

        Anchor commons = new Anchor("Общая информация");
        commons.addClickHandler(clickHandler);
        commons.getElement().setId("commons");

        Anchor structure = new Anchor("Структура базы знаний");
        structure.addClickHandler(clickHandler);
        structure.getElement().setId("structure");

        Anchor examples = new Anchor("Конкретные примеры");
        examples.addClickHandler(clickHandler);
        examples.getElement().setId("examples");
        Anchor article = new Anchor("Общая концепция подхода");
        article.getElement().setId("article");
        article.addClickHandler(clickHandler);

        commons_structure.add(commons);
        commons_structure.add(structure);

        examples_article.add(examples);
        examples_article.add(article);

        add(commons_structure);
        add(examples_article);

        //TODO: share url for access from the outside (for google reader)
        // URL: http://DOMAIN/AspectKnowledgeBaseApplication/pdfServlet
        frame = new Frame("http://docs.google.com/viewer?embedded=true&url="+GWT.getModuleBaseURL() + "pdfServlet?type=commons");
        frame.setStyleName("PDFframe");

        setStyleName("tabPanel");
        add(frame);
    }

    class AnchorClickHandler implements ClickHandler{
        @Override
        public void onClick(ClickEvent event) {

            Element e = ((Anchor)event.getSource()).getElement();

            if("examples".equals(e.getId())){
                AspectKnowledgeBaseApplication.get().showExamples();
            } else {
                frame.setUrl("http://docs.google.com/viewer?embedded=true&url="+GWT.getModuleBaseURL() + "pdfServlet?type="+e.getId());
            }

        }
    }
}
